package com.github.cakin.shiro.chapter23.client;

import com.github.cakin.shiro.chapter23.remote.RemoteServiceInterface;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;

import java.io.Serializable;

/**
 * @className: ClientSessionDAO
 * @description: Session 的维护通过远程暴露接口实现，即本地不维护会话。
 * @date: 2020/6/2
 * @author: cakin
 */
public class ClientSessionDAO extends CachingSessionDAO {
    /**
     * remoteService
     */
    private RemoteServiceInterface remoteService;
    /**
     * appKey
     */
    private String appKey;

    public void setRemoteService(RemoteServiceInterface remoteService) {
        this.remoteService = remoteService;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }
    /**
     * 功能描述：删除会话
     *
     * @author cakin
     * @date 2020/6/2
     * @param session 会话
     */
    @Override
    protected void doDelete(Session session) {
        remoteService.deleteSession(appKey, session);
    }
    /**
     * 功能描述：更新会话
     *
     * @author cakin
     * @date 2020/6/2
     * @param session 会话
     */
    @Override
    protected void doUpdate(Session session) {
        remoteService.updateSession(appKey, session);
    }

    /**
     * 功能描述：创建会话
     *
     * @author cakin
     * @date 2020/6/2
     * @param session 会话
     * @return Serializable 会话id
     */
    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = remoteService.createSession(session);
        assignSessionId(session, sessionId);
        return sessionId;
    }
    /**
     * 功能描述：创建会话
     *
     * @author cakin
     * @date 2020/6/2
     * @param sessionId 会话Id
     * @return Session 会话
     */
    @Override
    protected Session doReadSession(Serializable sessionId) {
        return remoteService.getSession(appKey, sessionId);
    }
}
